package cn.creditease.std.monitor.impl;

import cn.creditease.std.dao.JobDAO;
import cn.creditease.std.enums.JobStatusEnums;
import cn.creditease.std.model.Job;
import cn.creditease.std.monitor.AbstractRepairMonitor;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.util.CollectionUtils;

/**
 * @Description: 补偿那些处于处理中状态好长时间到任务
 * @Author: kai.yang
 * @Date: 2019-08-08 11:45
 */
public class RepairExpirePrecessingJobMonitor extends AbstractRepairMonitor {


    JobDAO jobDAO = JobDAO.newInstance();


    public RepairExpirePrecessingJobMonitor(String jobName) {
        super(jobName);
    }


    @Override
    public void check() {
        scheduledThreadPoolExecutor.scheduleAtFixedRate(() ->
        {

            try {
                List<Job> jobs = jobDAO.queryExpairJob(getJobName());
                if (!CollectionUtils.isEmpty(jobs)) {
                    //将任务已超时的任务，进行初始化
                    for (Job job : jobs) {
                        jobDAO.updateStatus(job.getId(), JobStatusEnums.PROCESS.toString(),
                            JobStatusEnums.INIT.toString());
                    }
                }

            } catch (Exception e) {

            }

        }, 60, 60 * 30, TimeUnit.SECONDS);
    }

}
